package com.kevin.hw.javaapi;

import org.apache.zookeeper.*;
import org.apache.zookeeper.data.Stat;

import java.io.IOException;
import java.util.concurrent.CountDownLatch;
import java.util.concurrent.TimeUnit;

/**
 * @author: kevin
 * @date: 2017/8/14
 * @description:
 */
public class ApiOperatorDemo implements Watcher{

    private final static String CONNECT_HOST = "localhost:2181";

    private static CountDownLatch countDownLatch = new CountDownLatch(1);

    private static ZooKeeper zookeeper;

    private static Stat stat = new Stat();



    public static void main(String[] args) throws IOException, InterruptedException, KeeperException {
        zookeeper = new ZooKeeper(CONNECT_HOST,5000,new ApiOperatorDemo());
        countDownLatch.await();

        /*String result = zookeeper.create("/node3","aaa".getBytes(), ZooDefs.Ids.OPEN_ACL_UNSAFE,CreateMode.PERSISTENT);
        zookeeper.getData("/node3",new ApiOperatorDemo(),stat);
        System.out.println("创建成功"+result);*/


        zookeeper.setData("/node3","kevin".getBytes(),-1);

        TimeUnit.SECONDS.sleep(5);

    }


    @Override
    public void process(WatchedEvent watchedEvent) {
        if (watchedEvent.getState() == Event.KeeperState.SyncConnected){

            if (Event.EventType.None == watchedEvent.getType() && watchedEvent.getPath() == null){
                countDownLatch.countDown();
                System.out.println("监控的状态-->>"+watchedEvent.getState() + "监控的时间类型->>"+watchedEvent.getType());
            }else if (Event.EventType.NodeDataChanged == watchedEvent.getType()){
                try {
                    System.out.println("数据变更触发的路径："+watchedEvent.getPath()+"改变后的值："+
                            zookeeper.getData(watchedEvent.getPath(),true,stat));
                } catch (KeeperException e) {
                    e.printStackTrace();
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
            }else if(Event.EventType.NodeCreated == watchedEvent.getType()){
                try {
                    System.out.println("新增的节点路径"+watchedEvent.getPath()+"创建的节点的值："+zookeeper.getData(watchedEvent.getPath(),true,stat));
                } catch (KeeperException e) {
                    e.printStackTrace();
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }

            }else if (Event.EventType.NodeDeleted == watchedEvent.getType()){
                System.out.println("节点删除路径："+watchedEvent.getPath());

            }else if(Event.EventType.NodeChildrenChanged == watchedEvent.getType()){

                try {
                    System.out.println("子节点数据变更路径："+watchedEvent.getPath()+"->节点的值："+
                            zookeeper.getData(watchedEvent.getPath(),true,stat));
                } catch (KeeperException e) {
                    e.printStackTrace();
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
            }

        }
    }
}
